package com.example.rental.controller;
import com.example.rental.entity.Houseinfo;
import com.example.rental.entity.dto.HouseDto;
import com.example.rental.result.Result;
import com.example.rental.service.HouseService;
import com.example.rental.service.OderService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/house")
@Api(tags = "用户操作接口文档",description = "登录,注册，查看用户,搜索用户，修改用户")
@ResponseBody
public class HouseController {

    @Autowired
    private HouseService houseService;
    @Autowired
    private OderService oderService;

    @PostMapping("/list")
    public Result list(@RequestBody HouseDto houseDto){
        Result list = houseService.list(houseDto);

        return list;
    }
    @PostMapping("/add")
    public Result addHouse(@RequestBody Houseinfo house){
        houseService.addHouse(house);
        return Result.ok(house);
    }
    @PostMapping("/update")
    public Result updateHouse(@RequestBody Houseinfo house){
        houseService.updateHouse(house);
        return Result.ok(house);
    }
    @PostMapping("/del")
    public Result deleteHouse(@RequestBody Houseinfo house){
        List<Integer> statusList = oderService.selectStatusByHouseId(house.getHouseid());
        for (Integer integer : statusList) {
            if(integer == 1){//有订单正在进行
                return Result.error(200,"有订单正在进行");
            }
        }
        houseService.deleteHouseid(house);
        return Result.ok(house);
    }
}

